<?
/**票付通对接票联
*2015-11-17 10:10
*by pzp
*测试环境接口地址：http://116.255.207.71:8080/utws/services/FSalerService?wsdl
*账户：FXTEST
*密钥：8c59d1214a659a7a6c277d371f8173d6
*商品名称：重渡沟景区
商品编码：SP000004
商品价格：95
*/
if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

class PiaoLian{
	
	public function __construct($tid){
		$sel = "select supplierIdentity,signkey from uu_qunar_use where DockingMode=4 and tid=$tid limit 1";
		$GLOBALS['le']->query($sel);
		$GLOBALS['le']->fetch_assoc();		
		$supplierIdentity = explode("|",$GLOBALS['le']->f('supplierIdentity'));  //查询票类id	  
		$this->AGENT_ID = $supplierIdentity[0];
		$this->AGENT_KEY = $GLOBALS['le']->f('signkey');
		$this->client = new SoapClient($supplierIdentity[1]);
	}
	
	//获取服务器时间
	public function getDateTime(){
		$tranData = '<?xml version="1.0" encoding="UTF-8" ?><tranData/>';		
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->getPlatDateTime($request)->out;
		$Check_IN = $this->Check_IN($reR);

		if((int)$Check_IN->repCode == 1){
			$platTime = (string)$Check_IN->platTime;
			return $platTime;
		}else{
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.json_encode($Check_IN));
			return 101;
		}
	}
	
	//获取产品信息
	public function getAgencyProductInfo(){
		$tranData = '<?xml version="1.0" encoding="UTF-8" ?><tranData><providerNo></providerNo><productCode></productCode><pageSize>50</pageSize><pageIndex>1</pageIndex></tranData>';		
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->getAgencyProductInfo($request)->out;
		$Check_IN = $this->Check_IN($reR);
		if((int)$Check_IN->repCode == 1){
			return $Check_IN;
		}else{
			$repCode = $Check_IN->repCode;
			$repMsg = $Check_IN->repMsg;
			$error_log = $this->logfile(date('Y-m-d H:i:s').'---'.$repCode.":".$repMsg);
			return 101;
		}
	
	}
	
	//订单新增
	public function agencyOrderAdd($request_array){
		$tranData = '<?xml version="1.0" encoding="UTF-8" ?><tranData><merOrderNo>'.$request_array['pftordernum'].'</merOrderNo><merOrderTime>'.date('Y-m-d H:i:s').'</merOrderTime><merOrderPayType>'.$request_array['ispay'].'</merOrderPayType><firstTourDate>'.$request_array['UUplaytime'].'</firstTourDate><ownerName>'.$request_array['pftordername'].'</ownerName><ownerIDCard>'.$request_array['IDcar'].'</ownerIDCard><ownerMobile>'.$request_array['UUordertel'].'</ownerMobile><takeTicketMode>1</takeTicketMode><ownerPwd></ownerPwd><ticketIDCards>'.$request_array['IDcar'].'</ticketIDCards><productCode>'.$request_array['uuid'].'</productCode><productPrice>'.$request_array['tprice'].'</productPrice><productNum>'.$request_array['UUtnum'].'</productNum></tranData>';

		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->agencyOrderAdd($request)->out;
		$Check_IN = $this->Check_IN($reR);
		if((int)$Check_IN->repCode == 1){
			$this->logfile($request_array[UUordertel]."|".(int)$Check_IN->repCode."|".(string)$Check_IN->repMsg."|".(string)$Check_IN->platOrderNo,'AddOrder_s');
			$GetOrderECode = $this->agencyGetOrderECode((string)$Check_IN->platOrderNo);
			$return = array(
				'platOrderNo'	=> (string)$Check_IN->platOrderNo,
				'GetOrderECode'	=> $GetOrderECode
			);
			return $return;
		}else{
			$this->logfile($request_array[UUordertel]."|".(int)$Check_IN->repCode."|".(string)$Check_IN->repMsg,'AddOrder_f');
			return 101;
		}
	}
	
	//订单电子码获取接口
	public function agencyGetOrderECode($platOrderNo){
		$tranData ='<?xml version="1.0" encoding="UTF-8" ?><tranData><platOrderNo>'.$platOrderNo.'</platOrderNo></tranData>';
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->agencyGetOrderECode($request)->out;
		$Check_IN = $this->Check_IN($reR);
		$this->logfile($platOrderNo."|".(int)$Check_IN->repCode."|".(string)$Check_IN->repMsg."|".(string)$Check_IN->elecCodeMode,'GetOrderECode');
		return (string)$Check_IN->elecCodeMode;
	}
	
	//撤改订单
	public function EditOrderNum($modify_array){
		$tranData ='<?xml version="1.0" encoding="UTF-8" ?><tranData><platOrderNo>'.$modify_array['APIOrdern'].'</platOrderNo><editMode>'.$modify_array['editMode'].'</editMode><productCode>'.$modify_array['uuid'].'</productCode><productNum>'.$modify_array['productNum'].'</productNum><elecCode></elecCode></tranData>';	
		$request['in0']= $this->ass_post($tranData);

		$reR = $this->client->agencyOrderEditProductNum($request)->out;
		$Check_IN = $this->Check_IN($reR);
		if((int)$Check_IN->repCode == 1){
			$this->logfile($modify_array['APIOrdern']."|".$modify_array['productNum']."|".(int)$Check_IN->repCode."|".(string)$Check_IN->repMsg,'editMode_s');
			return 100;
		}else{
			$this->logfile($modify_array['APIOrdern']."|".$modify_array['productNum']."|".(int)$Check_IN->repCode."|".(string)$Check_IN->repMsg,'editMode_f');
			return 101;
		}
	
	}
	
	//重发电子码
	public function agencyOrderRepeatECode($platOrderNo,$tel){
		$tranData ='<?xml version="1.0" encoding="UTF-8" ?><tranData><platOrderNo>'.$platOrderNo.'</platOrderNo><ownerMobile>'.$tel.'</ownerMobile><elecCode></elecCode></tranData>';		
		$request['in0']= $this->ass_post($tranData);
		
		$reR = $this->client->agencyOrderRepeatECode($request)->out;
		$Check_IN = $this->Check_IN($reR);
		$this->logfile($platOrderNo."|".$tel."|".(int)$Check_IN->repCode."|".(string)$Check_IN->repMsg,'resendcode');
	}
	
	//校验数据
	public function Check_IN($reR){
		$reR_xml = simplexml_load_string($reR);
		$reR_tranData = base64_decode((string)$reR_xml->tranData);
		$reR_signData = (string)$reR_xml->signData;
		$reR_tranData_md5 = md5($reR_tranData.$this->AGENT_KEY);
	
		if($reR_tranData_md5 == $reR_signData){
			$reR_tranData_xml= simplexml_load_string($reR_tranData);
			return $reR_tranData_xml;
		}else{
			return $reR_xml;
		}
	
	}
	
	/*共用方法*/
	//拼装数据
	public function ass_post($tranData){	
	
		$signed = md5($tranData.$this->AGENT_KEY);
		$tranData_base64 = base64_encode($tranData);
		
		$req_xml = '<?xml version="1.0" encoding="UTF-8" ?><req><interfaceVersion>1.0.0.0</interfaceVersion><merchantNo>' . $this->AGENT_ID . '</merchantNo><reqSerialNo>1</reqSerialNo><reqTime>' . date('Y-m-d H:i:s') . '</reqTime><merVar></merVar><tranData>' . $tranData_base64 . '</tranData><signData>' . $signed . '</signData></req>';
	    
		return $req_xml;
	}
	
	
	//日志
	public function logfile($word,$path){
		$filepath= '/var/www/html/new/d/logs/api/PiaoLian/'.$path.'.txt';
		$word = date("Y-m-d H:i:s")."|".$word;
		if(file_exists($filepath)){
			$maxsize  = 1024*1024*10;
			$filesize = filesize($filepath);
			$filetype = end(explode(".",$filepath));
			$filename = substr($filepath,0,strpos($filepath,'.'));
			if($filesize>$maxsize)
			rename($filepath, $filename.'_'.date('YmdHis').'.'.$filetype);
		}
		$fp = fopen($filepath,"a");
		flock($fp, LOCK_EX);
		fwrite($fp,$word."\n");
		flock($fp, LOCK_UN);
		fclose($fp);
	}
}
$pl = new PiaoLian(27521);
// $getTime  = $pl->getDateTime();
// $getAgencyProductInfo  = $pl->getAgencyProductInfo(); print_r($getAgencyProductInfo);
$request_array = array(
	'pftordernum'	=> 'pft20151117001004',
	'ispay'			=> 1,	
	'UUplaytime'	=> '2015-11-17',
	'pftordername'	=> '彭志萍',
	'IDcar'			=> '350321199108230123',
	'UUordertel'	=> '15959110796',
	'uuid'			=> 'SP000004',
	'tprice'		=> '90',
	'UUtnum'		=> '1',
);
// $agencyOrderAdd  = $pl->agencyOrderAdd($request_array); print_r($request_array);
$agencyGetOrderECode  = $pl->agencyGetOrderECode('9220091832'); print_r($request_array);

$modify_array = array(
	'APIOrdern'	=> '9220091832',
	'editMode'	=> 1,
	'uuid'		=> 'SP000004',
	'productNum'	=> 0
);
// $EditOrderNum  = $pl->EditOrderNum($modify_array); print_r($modify_array);
// $agencyOrderRepeatECode  = $pl->agencyOrderRepeatECode(); print_r($modify_array);
?>anData);

        $req_xml = '<?xml version="1.0" encoding="UTF-8" ?><req><interfaceVersion>1.0.0.0</interfaceVersion><merchantNo>' . $this->AGENT_ID . '</merchantNo><reqSerialNo>1</reqSerialNo><reqTime>' . date('Y-m-d H:i:s') . '</reqTime><merVar></merVar><tranData>' . $tranData_base64 . '</tranData><signData>' . $signed . '</signData></req>';

        return $req_xml;
    }

}
$pl = new PiaoLian(680);
// $getTime  = $pl->getDateTime();
// $getAgencyProductInfo  = $pl->getAgencyProductInfo(); print_r($getAgencyProductInfo);
// $request_array = array(
// 'pftordernum'	=> 'pft20151117001004',
// 'ispay'			=> 1,
// 'UUplaytime'	=> '2015-11-17',
// 'pftordername'	=> '彭志萍',
// 'IDcar'			=> '350321199108230123',
// 'UUordertel'	=> '15959110796',
// 'uuid'			=> 'SP000004',
// 'tprice'		=> '90',
// 'UUtnum'		=> '1',
// );
// $agencyOrderAdd  = $pl->agencyOrderAdd($request_array); print_r($request_array);
// $agencyGetOrderECode  = $pl->agencyGetOrderECode('9220091832'); print_r($request_array);

$modify_array = array(
'APIOrdern'	=> '2320102982',
'editMode'	=> 1,
'uuid'		=> 'SP816398',
'productNum'	=> 0
);
$EditOrderNum  = $pl->EditOrderNum($modify_array); print_r($EditOrderNum);
// $agencyOrderRepeatECode  = $pl->agencyOrderRepeatECode(); print_r($modify_array);
?>